package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.anno.MQDeviceCacheRefresh;
import com.jdrx.sw.sewage.beans.anno.UseDeviceCacheRefresh;
import com.jdrx.sw.sewage.beans.dto.AssetsCollectDTO;
import com.jdrx.sw.sewage.beans.dto.DeviceAlarmsQueryDTO;
import com.jdrx.sw.sewage.beans.dto.StockDevDTO;
import com.jdrx.sw.sewage.beans.vo.*;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.DeviceServiceImpl;
import com.jdrx.sw.sewage.service.MaintenanceServiceImpl;
import com.jdrx.sw.sewage.service.ProcessunitServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/assets")
@Api(value = "资产领用模块")
public class DeviceApi {

    @Autowired
    private DeviceServiceImpl deviceServiceImpl;
    @Autowired
    private ProcessunitServiceImpl processunitServiceImpl;
    @Autowired
    private MaintenanceServiceImpl maintenanceServiceImpl;

    @ApiOperation(value = "已使用设备")
    @RequestMapping(value = "selectStorageByStationId", method = RequestMethod.GET)
    public ResponseVO<Map<String, Object>> selectByPage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                        @RequestParam Long stationId) {
//        long start = System.currentTimeMillis();
        Map<String, Object> stringObjectMap = deviceServiceImpl.selectStorageByStationId(stationId);
//        System.out.println("一共使用时间：" + (System.currentTimeMillis() - start));
        return ResponseFactory2.ok(stringObjectMap);
    }
    /*@ApiOperation(value = "已使用设备")
    @RequestMapping(value = "selectStorageByStationId", method = RequestMethod.GET)
    public ResponseVO<Map<String, List<StorageDeviceVO>>> selectByPage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                        @RequestParam Long stationId) {
        long start = System.currentTimeMillis();
        Map<String, List<StorageDeviceVO>> stringListMap = deviceServiceImpl.selectStorageByStationId(stationId);
        System.out.println("一共使用时间：" + (System.currentTimeMillis() - start));
        return ResponseFactory2.ok(stringListMap);
    }*/

    @ApiOperation(value = "资产领用")
    @RequestMapping(value = "assetsCollect", method = RequestMethod.POST)
    @UseDeviceCacheRefresh
    @MQDeviceCacheRefresh
    public ResponseVO assetsCollect(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                    @RequestHeader(Constant.STATION_ID) String stationId,
                                    @RequestBody AssetsCollectDTO assetsCollectDTO) {

        return deviceServiceImpl.assetsCollect(assetsCollectDTO);
    }

    @ApiOperation(value = "根据物料和品类查询规格")
    @RequestMapping(value = "selectDossierSpec", method = RequestMethod.GET)
    public ResponseVO<List<String>> selectDossierSpec(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                      @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                      @RequestParam("materielId") Long materielId,
                                                      @RequestParam("categoryId") Long categoryId) {
        return ResponseFactory2.ok(processunitServiceImpl.selectDossierSpec(materielId, categoryId));
    }

    @ApiOperation(value = "根据站点查询工艺单元、采集名称")
    @RequestMapping(value = "selectByStationId", method = RequestMethod.GET)
    public ResponseVO<List<ProcessunitVO>> selectByStationId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                             @RequestParam("stationId") Long stationId) {
        return ResponseFactory2.ok(processunitServiceImpl.selectByStationId(stationId));
    }

    @ApiOperation(value = "根据物料、品类、规格获取库存设备")
    @RequestMapping(value = "selectByMeIdAndCaIdAndDs", method = RequestMethod.POST)
    public ResponseVO<List<StockDevVO>> selectByMeIdAndCaIdAndDs(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                 @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                 @RequestBody StockDevDTO stockDevDTO) {
        return ResponseFactory2.ok(processunitServiceImpl.selectByMeIdAndCaIdAndDs(stockDevDTO));
    }

    @ApiOperation(value = "通过Id查询设备VO")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public ResponseVO<DeviceVO> getById(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                        @RequestParam Long deviceId) {
        return ResponseFactory2.ok(deviceServiceImpl.getById(deviceId));
    }

    @ApiOperation(value = "查询告警列表")
    @RequestMapping(value = "selectAlarmsByDevId", method = RequestMethod.POST)
    public ResponseVO<List<DeviceAlarmQueryPageVO>> selectAlarmsByDevId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                        @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                        @RequestBody DeviceAlarmsQueryDTO queryDTO) {
        return ResponseFactory2.ok(deviceServiceImpl.selectAlarmsByDevId(queryDTO));
    }

    @ApiOperation(value = "保养/维修记录列表  类型（1 - 保养 2 -维修）")
    @RequestMapping(value = "findMaintenanceRecordList", method = RequestMethod.GET)
    public ResponseVO<List<MaintenanceRecordVO>> findMaintenanceRecordList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                           @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                           @RequestParam("type") Integer type,
                                                                           @RequestParam("devId") Long devId) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findMaintenanceRecordLists(type, devId));
    }
}
